package org.example.chat.dao;

import org.example.chat.entity.FriendRelation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FriendRelationRepository extends JpaRepository<FriendRelation, String> {

    /**
     * 根据id查询好友id集合
     * @param username
     * @return
     */
    @Query(value = "select t.friend_id, t.note  from friend_relation t where t.user_id = ?1", nativeQuery = true)
    List<Object[]> findFriendIdsByUserId(String username);

    /**
     * 根据用户ID和好友id查询好用关系
     * @param userId
     * @param friend
     * @return
     */
    @Query(value = "select t.* from friend_relation t where t.user_id = ?1 and t.friend_id = ?2 union select t.* from friend_relation t where t.user_id = ?2 and t.friend_id = ?1", nativeQuery = true)
    List<Object[]> findRelation(String userId, String friend);
}
